package com.homegoods.designer.ui.activity;

import android.os.Bundle;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.homegoods.aroutercomment.ARouterPath;
import com.homegoods.aroutercomment.UserService;
import com.homegoods.designer.R;
import com.homegoods.designer.model.DesignerCaseModel;
import com.homegoods.designer.presenter.DesignerMoreCasePresenter;
import com.kedll.library.adapter.MultiTypeAdapter;
import com.kedll.library.base.KRefreshListActivity;
import com.kedll.library.databinding.GeneralIncludeActionbarAndListBinding;
import com.kedll.library.dialog.ECAlertDialog;
import com.kedll.library.util.MyUtils;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author QiQi
 * @ClassName: MyDesignerCaseActivity
 * @date 2018/9/17
 * @description 我的作品
 */
@Route(path = ARouterPath.PATH_MY_DESIGN)
public class MyDesignerCaseActivity extends KRefreshListActivity<GeneralIncludeActionbarAndListBinding, DesignerMoreCasePresenter> {

    @Autowired
    public UserService userService;

    @Override
    protected void initToolBar(Bundle savedInstanceState) {
        super.initToolBar(savedInstanceState);
        ARouter.getInstance().inject(this);
        setTitle("我的作品");
        setTopRightButton("", R.mipmap.designer_add_case,
                //跳转到编辑作品
                v -> ARouter.getInstance()
                        .build(ARouterPath.PATH_EDIT_CASE_1)
                        .navigation());
    }

    @Override
    protected DesignerMoreCasePresenter getPresenter() {
        return new DesignerMoreCasePresenter(this);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
        ARouter.getInstance().inject(this);
        appBarLayout.setBackgroundResource(R.color.color_f6f6f6);
        refreshLayout.setBackgroundResource(R.color.white);
        list.setPadding(MyUtils.dp2px(16), MyUtils.dp2px(16), MyUtils.dp2px(16), 0);
        list.setClipToPadding(false);
        refreshLayout.setClipToPadding(false);
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        adapter.setOnItemClickListener((MultiTypeAdapter.OnItemClickListener<DesignerCaseModel>) (v, iItem) -> {
            if (v.getId() == R.id.ib_del) {
                int index = adapter.findPos(iItem);
                ECAlertDialog.buildAlert(MyDesignerCaseActivity.this, "确定删除此作品?", (dialog, which) -> {
                    //模拟删除接口调用
                    Observable.just(iItem.getData().getId())
                            .compose(bindToLifecycle())
                            .delay(100, TimeUnit.MILLISECONDS)
                            .subscribeOn(Schedulers.io())
                            .observeOn(AndroidSchedulers.mainThread())
                            .subscribe(s -> {
                                adapter.removeItem(iItem);
                                adapter.notifyItemRemoved(index);
                            });
                }).show();
            } else {

            }
        });
    }

    @Override
    protected int getLayout() {
        return R.layout.general_include_actionbar_and_list;
    }
}
